<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<c:set value="recruit" var="moduleName" scope="request" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>在线申请 - 投资规划</title>
	</head>
	<body>
		<html:form action="/site/user/applyPlan.do" styleId="mainForm" method="POST">
			<input type="hidden" name="method" class="method" value="save" />
			<html:hidden property="id" />
			<html:hidden property="applyBaseId" />
			<input type="hidden" name="direction" id="direction" value="" />
			<div class="site-title">
				七、 投资规划
			</div>
			<br />
			<div class="div-content">
			<table class="table-layout">
				<tr class="tr-even">
					<td>
						如果本申请获得批准，本单位可动用的资金不少于
						<html:text property="investLowerLimit" styleId="investLowerLimit" readonly="true" styleClass="input-readonly" />
						万元<span class="required">*</span>
					</td>
				</tr>
				<tr class="tr-odd">
					<td>
						其中，自有资金
						<html:text property="investOwn" styleId="investOwn" onkeyup="calInvestLower()"/>
						万元<span class="required">*</span>，融资
						<html:text property="investFinancing" styleId="investFinancing" onkeyup="calInvestLower()"/>
						万元<span class="required">*</span>
					</td>
				</tr>
				<tr class="tr-even">
					<td>
						如果本申请获得批准，未来斯柯达4S店的股本总额拟为
						<html:text property="planTotalAssets" styleId="planTotalAssets"  styleClass="input-readonly"/>
						万元<span class="required">*</span>
					</td>
				</tr>
				<tr class="tr-odd">
					<td>
						股本结构如下：<span class="required">*</span>
					</td>
				</tr>
				<tr class="tr-even">
					<td colspan="2">
						<table class="table-layout" id="tableShareholderC">
							<thead>
								<tr class="tr-even">
									<td>
										出资方
									</td>
									<td style="width:150px;">
										出资额（万元）
									</td>
									<td style="width:150px;">
										所占比例（%）
									</td>
									<th style="width:100px;">
										操作
									</th>
								</tr>
							</thead>
							<tbody id="tbodyShareholderC"></tbody>
						</table>
						<div id="divShareholderCFormContainer">
						</div>
						<br />
						<div class="center-opertion">
							<input type="button" value="添加" onclick="preAddShareholderC()" />
						</div>
					</td>
				</tr>
				<tr class="tr-odd">
					<td>
						如果本申请获得批准，贵公司未来三年的经营规划（从下一年度开始）：

					</td>
				</tr>
			</table>
			<table class="table-layout">
				<tr class="tr-even">
					<td>
						&nbsp;
					</td>
					<td>
						<html:text property="planYear1" styleClass="Wdate"  onclick="WdatePicker({dateFmt:'yyyy'})" />
						年

					</td>
					<td>
						<html:text property="planYear2" styleClass="Wdate"  onclick="WdatePicker({dateFmt:'yyyy'})" />
						年

					</td>
					<td>
						<html:text property="planYear3" styleClass="Wdate"  onclick="WdatePicker({dateFmt:'yyyy'})" />
						年

					</td>
				</tr>
				<tr class="tr-odd">
					<td>
						销售台数（台）
					</td>
					<td>
						<html:text property="planSalesVolume1" />
					</td>
					<td>
						<html:text property="planSalesVolume2" />
					</td>
					<td>
						<html:text property="planSalesVolume3" />
					</td>
				</tr>
				<tr class="tr-even">
					<td>
						修理台次（台）

					</td>
					<td>
						<html:text property="planServiceVolume1" />
					</td>
					<td>
						<html:text property="planServiceVolume2" />
					</td>
					<td>
						<html:text property="planServiceVolume3" />
					</td>
				</tr>
			</table>
			<table class="table-layout">
				<tr class="tr-odd">
					<td>
						其它内容
					</td>
				</tr>
				<tr class="tr-even">
					<td>
						<html:textarea property="notes" style="width:100%" />
					</td>
				</tr>
			</table>
			<br />
			<div class="center-opertion">
				<html:submit>保存</html:submit>
				<input type="button" value="上一步" onclick="toStep(-1)" />
				<input type="button" value="下一步" onclick="toStep(1)" />
				<html:cancel>取消</html:cancel>
			</div>
			</div>
		</html:form>
		<form action="<c:url value="/site/user/shareholderC.do?method=ajaxSave" />" method="POST" id="shareholderCForm" style="display:none;" target="hiddenFrame">
			<input type="hidden" name="id" />
			<input type="hidden" name="applyPlanId" value="<c:out value="${applyPlanForm.id}" />" id="currentEquityId" />
			<table class="table-layout">
				<tr class="tr-odd">
					<td>
						<input type="text" name="shareholderName" />
					</td>
					<td style="width:150px;">
						<input type="text" name="stake" onkeyup="calPercent('C')"/>
					</td>
					<td style="width:150px;">
						<input type="text" name="percent" readonly="true" class="input-readonly"/>
					</td>
					<td style="width:100px;">
						<input type="submit" value="提交" />
						<input type="button" value="取消" onclick="hideShareholderC()" />
					</td>
				</tr>
			</table>
		</form>
		<iframe name="hiddenFrame" style="display:none;"></iframe>
		<script type="text/javascript">
function validate100(tbodyId){
	var ts = _g(tbodyId).childNodes;
	var sum = 0;
	/*if(ts && ts.length > 0){
		for(var i = 0;i< ts.length;i++){
			x = new Number(ts[i].childNodes[2].innerHTML);
			if(!isNaN(x)) sum += x;
		}
	}
	if(sum > 100){
		alert("股结构所占比例之和不能大于100。")
		return false;
	}*/
	return true;
}
function validate(){
	if(validate100("tbodyShareholderC"))
		_g("mainForm").submit();
}
function toStep(num){
	if(!validate100("tbodyShareholderC")) return false;

	if(applyPlanIdVal != null && applyPlanIdVal.length > 0)
	_ajax(shareholderCUrl + "?method=ajaxList&applyPlanId=" + applyPlanIdVal,function(xhr){
		var shareholderCList = JSON.parse(xhr.responseText);
		shareholderCList = shareholderCList.data;

		if(shareholderCList && shareholderCList.length > 0){
			if(num > 0)
				_g("direction").value="next";
			else if(num < 0)
				_g("direction").value="last";
		
			_g("mainForm").submit();
		}
		else
			alert("请填写股本结构");
	})
	else
		alert("请先保存该页表单，再填写股本结构");
}

var applyPlanIdVal = _g("currentEquityId").value;

//shareholderC
var shareholderCUrl = "<c:url value='shareholderC.do' />";
var shareholderCFormEl = _g("shareholderCForm");
var shareholderCFlag=false;
function preAddShareholderC(){
	if(applyPlanIdVal == null || applyPlanIdVal.length < 1)
		alert("请先保存当前表单，并至少添加1个股东。")
	else if(!shareholderCFlag){
		showShareholderC();				
	}else{
		shareholderCFormEl.submit();		
	}
}

function resetShareholderC(){
	shareholderCFormEl.reset();
	shareholderCFormEl["id"].value="";
}

function showShareholderC(){
	resetShareholderC();
	_g("divShareholderCFormContainer").appendChild(shareholderCFormEl);
	shareholderCFormEl.style.display="";
	shareholderCFlag=true;
}

function hideShareholderC(){
	resetShareholderC();
	shareholderCFormEl.style.display = "none";
	_g('bd').appendChild(shareholderCFormEl);
	shareholderCFlag=false;
	calPercent('C');
}

function editShareholderC(id){
	shareholderCFlag=true;
	_ajax(shareholderCUrl + "?method=ajaxView&id=" + id,function(xhr){
		var shareholderC = JSON.parse(xhr.responseText);
		if(!refreshMessage(shareholderC)) return false;
		shareholderC = shareholderC.data;
		
		showShareholderC();
		shareholderCFormEl.id.value = shareholderC.id;
		shareholderCFormEl.shareholderName.value = shareholderC.shareholderName;
		shareholderCFormEl.stake.value = shareholderC.stake;
		shareholderCFormEl.percent.value = shareholderC.percent;
	})
}
function getShareHolderRecord(flag){
	var rec=_g('tbodyShareholder'+flag);
	return rec;
}
function getTotalInfo(rec,id){
	var total=0;	
	for(var i=0;i<rec.length;i++){
		var tr=rec[i];
		var temp=tr.childNodes[1].firstChild.nodeValue;
		var tempid=tr.childNodes[3].lastChild.firstChild.nodeValue;
		if(id==null||id!=tempid)
			total=total*1+temp*1;		
	}	
	return total;
}
function calPercent(flag){
	var formName="shareholder"+flag+"Form";
	var stake=_g(formName)["stake"].value;
	var rec=getShareHolderRecord(flag).childNodes;
	var total=0;	
	var shareID=_g(formName)["id"].value;	
	if(shareID=="")
		total=getTotalInfo(rec,null)*1.0+stake*1.0;	
	else	
		total=getTotalInfo(rec,shareID)*1.0+stake*1.0;	
	if(rec.length&&rec.length!=0){
		var assetsObj=_g("planTotalAssets");
		assetsObj.value=total;
	}
	if(total==0||rec.length==0){
		_g(formName)["percent"].value="100";
	}else{
		for(var i=0;i<rec.length;i++){
			var tr=rec[i];
			var tempstake=tr.childNodes[1].firstChild.nodeValue*1;
			var temppercent=tr.childNodes[2].firstChild;		
			var tempid=tr.childNodes[3].lastChild.firstChild.nodeValue;
			if(shareID==""||shareID!=tempid)
				temppercent.nodeValue=total==0?0:Math.round(((tempstake/total)*10000))/100;
		}
	}
	_g(formName)["percent"].value=total==0?0:Math.round(((stake/total)*10000))/100;;
}
function successShareholderC(jsonString){
	if(!refreshMessageJsonString(jsonString)){ shareholderCFlag=true; return false;}
	hideShareholderC();
	listShareholderC();
	showShareholderC();
}
	function calInvestLower(){
		var investLower=_g("investLowerLimit");
		var investOwnValue=_g("investOwn").value;
		var investFinancingValue=_g("investFinancing").value;
		investLower.value=investOwnValue*1+investFinancingValue*1;
	}

function deleteShareholderC(id){
	if(confirm("确定要删除该记录吗？")) 
		_ajax(shareholderCUrl + "?method=ajaxErase&id="+id, function(xhr){
			var data = JSON.parse(xhr.responseText);
			if(!refreshMessage(data)) return false;
			
			listShareholderC();
		})
	calPercent('C');
}

function listShareholderC(){
	if(applyPlanIdVal != null && applyPlanIdVal.length > 0)
	_ajax(shareholderCUrl + "?method=ajaxList&applyPlanId=" + applyPlanIdVal,function(xhr){
		var shareholderCList = JSON.parse(xhr.responseText);
		if(!refreshMessage(shareholderCList,false)) return false;
		shareholderCList = shareholderCList.data;

		var listHtml = '<table><tbody id="tbodyShareholderC">';
		if(shareholderCList && shareholderCList.length)
		for(var i = 0;i < shareholderCList.length;i++){
			listHtml += '<tr class="'+(i%2 == 0 ? 'tr-odd' : 'tr-even')+'">';
			listHtml += '<td>'+shareholderCList[i].shareholderName+'</td>';
			listHtml += '<td>'+shareholderCList[i].stake+'</td>';
			listHtml += '<td>'+shareholderCList[i].percent+'</td>';
			listHtml += '<td>';
			listHtml += '<a href="javascript:void(0);" onclick="javascript:editShareholderC('+shareholderCList[i].id+');">修改</a>&nbsp;';
			listHtml += '<a href="javascript:void(0)" onclick="javascript:deleteShareholderC('+shareholderCList[i].id+');">删除</a>&nbsp;';
			listHtml += '<span style="display:none">';
			listHtml += shareholderCList[i].id;
			listHtml += '</span>';
			listHtml += '</td>';			
			listHtml += '</tr>';
		}
		listHtml += '</tbody></table>';
		equalInnerHTML(listHtml,function(tbody){
			var t = _g("tableShareholderC");
			var tb =  _g("tbodyShareholderC");
			t.removeChild(tb);
			t.appendChild(tbody);
			calPercent('C');
		})
	})
}
listShareholderC();


		</script>
	</body>
</html>
